package club.ljwang.service;

import club.ljwang.constant.MessageConstant;
import club.ljwang.dao.CheckGroupMapper;
import club.ljwang.dao.CheckItemMapper;
import club.ljwang.entity.PageResult;
import club.ljwang.entity.QueryPageBean;
import club.ljwang.entity.Result;
import club.ljwang.pojo.CheckGroup;
import club.ljwang.pojo.CheckItem;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: wangl
 * @Date: 2020/2/25 23:06
 * @Description:
 */

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {

        checkGroupMapper.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {

                checkGroupMapper.setCheckGroupAndCheckItem(checkitemId, checkGroupId);

            }
        }


    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {

        Integer checkGroupId = checkGroup.getId();
        checkGroupMapper.deleteRelationship(checkGroupId);
        checkGroupMapper.edit(checkGroup);
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {

                checkGroupMapper.setCheckGroupAndCheckItem(checkitemId, checkGroupId);

            }
        }

    }

    @Override
    public Result findAll() {

        List<CheckGroup> groupList = checkGroupMapper.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,groupList);
    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.告诉Mybatis我们要分页
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.写一个跟分页毫无关系的sql
        List<CheckGroup> groupList = checkGroupMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), groupList);
    }

    @Override
    public Result findByIdForEdit(Integer id) {
        Map map = new HashMap();
        List<CheckItem> checkItems = checkItemMapper.findAll();
        List<Integer> checkitemIds = checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
        CheckGroup checkGroup = checkGroupMapper.findCheckGroupById(id);

        map.put("checkItems", checkItems);
        map.put("checkitemIds", checkitemIds);
        map.put("checkGroup", checkGroup);

        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS, map);
    }


}
